package solved;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
ID:		liluyan1
LANG:	JAVA
TASK:	inflate
*/
public class inflate {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("inflate.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"inflate.out")));
		new inflate().solve(f, out);
		out.close();
		System.exit(0);
	}
	
	private void solve(BufferedReader f, PrintWriter out) throws IOException {
		int[] a = read(f, 2);
		int M = a[0];
		int N = a[1];
		
		int[] value = new int[N];
		int[] time = new int[N];
		for (int i=0; i<N; i++) {
			a = read(f, 2);
			value[i] = a[0];
			time[i] = a[1];
		}
		
		int[] dp = new int[M+1];
		for (int i=0; i<N; i++) {
			for (int j=0; j<=M-time[i]; j++) {
				dp[j+time[i]] = Math.max(dp[j+time[i]], dp[j] + value[i]);
			}
		}
		
		out.println(dp[M]);
	}
	
	public int read(BufferedReader f) throws IOException {
		return Integer.parseInt(f.readLine());
	}
	
	public int[] read(BufferedReader f, int N) throws IOException {
		String[] t = f.readLine().split("\\s+");
		int[] a = new int[N];
		for (int i=0; i<N; i++) {
			a[i] = Integer.parseInt(t[i]);
		}
		return a;
	}
}
